<?php

require( "functions.php" );

$topNode = LoggerCreateXMLDocument();

if ( LoggerOpenDB() )
{
	if ( isset( $_POST[ "net_id" ], $_POST[ "logger_id" ], $_POST[ "activity_time" ], $_POST[ "type_code" ],
		$_POST[ "type_descrip" ], $_POST[ "details" ], $_POST[ "activity_id" ], $_POST[ "is_priority" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		if ( isset( $_POST[ "roster_id" ] ) )
		{
			$roster_id = "'" . mysql_real_escape_string( rawurldecode( $_POST[ "roster_id" ] ) ) . "'";
		}
		else
		{
			$roster_id = "NULL";
		}
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$activity_time = mysql_real_escape_string( rawurldecode( $_POST[ "activity_time" ] ) );
		if ( isset( $_POST[ "location" ] ) )
		{
			$location = "'" . mysql_real_escape_string( utf8_decode( rawurldecode( $_POST[ "location" ] ) ) ) . "'";
			$latitude = ( double ) $_POST[ "latitude" ];
			$longitude = ( double ) $_POST[ "longitude" ];
		}
		else
		{
			$location = "NULL";
			$latitude = 0;
			$longitude = 0;
		}
		$type_code = ( int ) $_POST[ "type_code" ];
		$type_descrip = mysql_real_escape_string( rawurldecode( $_POST[ "type_descrip" ] ) );
		$details = mysql_real_escape_string( utf8_decode( rawurldecode( $_POST[ "details" ] ) ) );
		$activity_id = mysql_real_escape_string( rawurldecode( $_POST[ "activity_id" ] ) );
		$is_priority = ( int ) $_POST[ "is_priority" ];
		if ( isset( $_POST[ "needs_ack" ] ) )
			$needs_ack = 1;
		else
			$needs_ack = 0;
		$sql = "INSERT INTO activity ( net_id, roster_id, logger_id, activity_time, location, latitude, longitude, type_code, type_descrip, details, activity_id, is_priority, needs_ack ) " .
			"VALUES ( $net_id, $roster_id, '$logger_id', '$activity_time', $location, $latitude, $longitude, $type_code, '$type_descrip', '$details', '$activity_id', $is_priority, $needs_ack )";
		if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
			$error = "Activity insert failure";
	}
	else if ( isset( $_POST[ "net_id" ], $_POST[ "logger_id" ], $_POST[ "activity_id" ], $_POST[ "needs_ack" ] ) )
	{
		$net_id = ( int ) $_POST[ "net_id" ];
		$logger_id = mysql_real_escape_string( rawurldecode( $_POST[ "logger_id" ] ) );
		$activity_id = mysql_real_escape_string( rawurldecode( $_POST[ "activity_id" ] ) );
		$sql = "UPDATE activity SET needs_ack = 0 WHERE net_id = $net_id AND logger_id = '$logger_id' AND activity_id = '$activity_id'";
		if ( mysql_query( $sql ) == FALSE || mysql_affected_rows() != 1 )
			$error = "Activity needs_ack update failure";
	}
	else
	{
		$error = "Submission failure";
	}
}
else
{
	$error = "DB open failure";
}

if ( isset( $error ) )
{
	LoggerAppendError( $topNode, $error );
}

$doc = $topNode->ownerDocument;
echo $doc->saveXML();

?>